// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function() { // 通用预处理器

	},
	_timing: function(param) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo(),
		});
		return setLoopTask(dbJQL)
	}
	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	/* 
	method1(param1) {
		// 参数校验，如无参数则不需要
		if (!param1) {
			return {
				errCode: 'PARAM_IS_NULL',
				errMsg: '参数不能为空'
			}
		}
		// 业务逻辑
		
		// 返回结果
		return {
			param1 //请根据实际需要返回值
		}
	}
	*/
}
async function setLoopTask(dbJQL) {
	const loopRes = await dbJQL.collection('task_list').where(
		`"killFlag == '0' && delFlag == '0' && taskRepeat != '1' "`).get()
	// 获取到所有的循环任务
	const {
		data
	} = loopRes
	const len = data.length
	const toDay = dayjs();
	// 今日的时分
	const now_hour = toDay.hour()
	const now_min = toDay.minute()
	const now_week = toDay.week()
	const now_date = toDay.date()
	// 
	for (let i = 0; i < Things.length; i++) {
		const item = data[i]
		const taskRepeat = item.taskRepeat
		const {
			startTime,
			endTime
		} = item

		const startDay = dayjs(startTime)
		const endDay = dayjs(endTime)
		// 获取小时
		const start_hour = startDay.hour()
		const end_hour = endDay.hour()
		// 获取分钟
		const start_min = startDay.minute()
		const end_min = endDay.minute()
		//  获取周
		const start_week = startDay.day()
		const end_week = endDay.day()
		// 获取日
		const start_date = startDay.date()
		const end_date = startDay.date()
		// 获取 时分
		// 新增一个定时任务
		if (taskRepeat == '2') {
			// copy当前项目
			insertTodayTask(dbJQL, item, {
				start_hour,
				end_hour,
				start_min,
				end_min
			})
		} else if (taskRepeat == '3' && start_week === now_week) { // 每周
			insertWeekTask(dbJQL, item, {
				start_hour,
				start_week,
				end_week,
				end_hour,
				start_min,
				end_min
			})

		} else if (taskRepeat === '4' && start_date === now_date) { //  每月
			insertDayTask(dbJQL, item, {
				start_hour,
				start_date,
				end_date,
				end_hour,
				start_min,
				end_min
			})
		}
	}

}

function getSubList(dbJQL) {
	return
}

function insertTodayTask(dbJQL, data, time) {
	// 
	const toDay = dayjs();
	const {
		start_hour,
		end_hour,
		start_min,
		end_min
	} = time
	const todyTask = JSON.parse(JSON.stringify(data))
	delete todyTask._id
	const newTaskId = uuid.v4();
	// 保存父级的 id
	todyTask.fid = todyTask.tid
	todyTask.tid = newTaskId
	todyTask.startTime = toDay.hour(start_hour).minute(start_min)
	todyTask.endTime = toDay.hour(end_hour).minute(end_min)
	return dbJQL.collection('task_list').add({
		...todyTask
	})
}

function insertWeekTask(dbJQL, data, time) {
	const toDay = dayjs();
	const {
		start_hour,
		start_week,
		end_week,
		end_hour,
		start_min,
		end_min
	} = time
	const todyTask = JSON.parse(JSON.stringify(data))
	delete todyTask._id
	const newTaskId = uuid.v4();
	// 保存父级的 id
	todyTask.fid = todyTask.tid
	todyTask.tid = newTaskId
	todyTask.startTime = toDay.hour(start_hour).minute(start_min)
	todyTask.endTime = toDay.day(end_week).hour(hour).minute(end_min)
	return dbJQL.collection('task_list').add({
		...todyTask
	})
}

function insertDayTask(dbJQL, data, time) {
	const toDay = dayjs();
	const {
		start_hour,
		start_date,
		end_date,
		end_hour,
		start_min,
		end_min
	} = time
	const todyTask = JSON.parse(JSON.stringify(data))
	delete todyTask._id
	const newTaskId = uuid.v4();
	// 保存父级的 id
	todyTask.fid = todyTask.tid
	todyTask.tid = newTaskId
	todyTask.startTime = toDay.hour(start_hour).minute(start_min)
	todyTask.endTime = toDay.date(end_date).hour(hour).minute(end_min)
	return dbJQL.collection('task_list').add({
		...todyTask
	})
}